package com.dayu.recovery.domain.data;

import com.dayu.common.annotation.Excel;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.util.ObjectUtils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 商品时间记录表
 */
public class GoodsTimeData {

    /** 商品编码*/
    @Excel(name = "商品编码")
    private String goodsId;

    /** 回收编号*/
    @Excel(name = "回收编号")
    private Long collectId;

    /** 型号*/
    @Excel(name = "型号")
    private String goodsName;

    /** IMEI*/
    @Excel(name = "IMEI")
    private String goodsSn;

    /** 规格*/
    @Excel(name = "规格")
    private String printing;

    /** 回收时间*/
    @Excel(name = "回收时间", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date collectTime;

    /** 生成时间*/
    @Excel(name = "生成时间", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    /** 验机时间*/
    @Excel(name = "验机时间", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date testTime;

    /** 步骤id*/
    private Long stepLogId;

    /** 步骤类型*/
    private Integer stepType;

    /** 步骤创建时间*/
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date stepCreateTime;

    /** 步骤确定时间*/
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date stepUpdateTime;

    /** 出入库日志id*/
    private Long stockLogId;

    /** 入库时间*/
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date stockCreateTime;

    /** 确定入库时间*/
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date stockUpdateTime;

    /** 销售记录*/
    private Long billId;

    /** 销售时间*/
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date billCreateTime;

    /** 销售审核时间*/
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date billUpdateTime;

    private List<Map<String, Object>> stepList;

    private List<Map<String, Object>> billList;
    @Excel(name = "步骤列表")
    private String step;

    @Excel(name = "账单列表")
    private String bill;

    private Map<String, Object> params;

    public void builderByList(List<GoodsTimeData> list) {
        if(ObjectUtils.isEmpty(list)) {
            return;
        }
        if(list.size() == 0) {
            return;
        }
        Set<Long> stepId = new HashSet<>();
        Set<Long> billId = new HashSet<>();
        this.billList = new ArrayList<>();
        this.stepList = new ArrayList<>();
        for(GoodsTimeData data : list) {
            if(stepId.add(data.getStepLogId())) {
                //添加成功,代表不是重复数据
                Map<String, Object> map = new HashMap<>();
                map.put("stepLogId", data.getStepLogId());
                map.put("stepType", data.getStepType());
                map.put("createTime", data.getStepCreateTime());
                map.put("updateTime", data.getStepUpdateTime());
                this.stepList.add(map);
            }

            if(billId.add(data.getBillId())) {
                Map<String, Object> map = new HashMap<>();
                map.put("billId", data.getBillId());
                map.put("createTime", data.getBillCreateTime());
                map.put("updateTime", data.getBillUpdateTime());
                this.billList.add(map);
            }
        }

        this.goodsId = list.get(0).getGoodsId();
        this.goodsName = list.get(0).getGoodsName();
        this.goodsSn = list.get(0).getGoodsSn();
        this.printing = list.get(0).getPrinting();
        this.collectId = list.get(0).getCollectId();
        this.createTime = list.get(0).getCreateTime();
        this.testTime = list.get(0).getTestTime();
        this.collectTime = list.get(0).getCollectTime();
    }

    public void tranString() {
        if(ObjectUtils.isEmpty(stepList)) {
            return;
        }
        StringBuilder s = new StringBuilder();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(Map<String, Object> m : stepList) {
            s.append(stepType(Integer.parseInt(m.get("stepType").toString()))).append("\r\n");
            if(!ObjectUtils.isEmpty(m.get("createTime")))
                s.append(dateFormat.format((Date) m.get("createTime"))).append("\r\n");
            if(!ObjectUtils.isEmpty(m.get("updateTime")))
                s.append(dateFormat.format((Date) m.get("updateTime"))).append("\r\n");

        }

        this.step = s.toString();

        if(ObjectUtils.isEmpty(billList)) {
            return;
        }
        StringBuilder b = new StringBuilder();
        for (Map<String, Object> m : billList) {
            b.append("账单编号: ").append(m.get("billId")).append("\r\n");
            if(!ObjectUtils.isEmpty(m.get("createTime")))
            b.append(dateFormat.format((Date) m.get("createTime"))).append("\r\n");
            if(!ObjectUtils.isEmpty(m.get("updateTime")))
            b.append(dateFormat.format((Date) m.get("updateTime"))).append("\r\n");
        }
        this.bill = b.toString();
    }

    private String stepType(Integer type) {
        switch (type) {
            case 1 : return "移交待验机";
            case 2 : return "移交待报价";
            case 3 : return "移交待入库";
            case 4 : return "驳回验机";
            case 5 : return "驳回报价";
        }
        return "";
    }

    public String getStep() {
        return step;
    }

    public void setStep(String step) {
        this.step = step;
    }

    public String getBill() {
        return bill;
    }

    public void setBill(String bill) {
        this.bill = bill;
    }

    public Map<String, Object> getParams() {
        return params;
    }

    public List<Map<String, Object>> getStepList() {
        return stepList;
    }

    public void setStepList(List<Map<String, Object>> stepList) {
        this.stepList = stepList;
    }

    public List<Map<String, Object>> getBillList() {
        return billList;
    }

    public void setBillList(List<Map<String, Object>> billList) {
        this.billList = billList;
    }

    public void setParams(Map<String, Object> params) {
        this.params = params;
    }

    public String getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(String goodsId) {
        this.goodsId = goodsId;
    }

    public Long getCollectId() {
        return collectId;
    }

    public void setCollectId(Long collectId) {
        this.collectId = collectId;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public String getGoodsSn() {
        return goodsSn;
    }

    public void setGoodsSn(String goodsSn) {
        this.goodsSn = goodsSn;
    }

    public String getPrinting() {
        return printing;
    }

    public void setPrinting(String printing) {
        this.printing = printing;
    }

    public Date getTestTime() {
        return testTime;
    }

    public void setTestTime(Date testTime) {
        this.testTime = testTime;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getCollectTime() {
        return collectTime;
    }

    public void setCollectTime(Date collectTime) {
        this.collectTime = collectTime;
    }

    public Long getStepLogId() {
        return stepLogId;
    }

    public void setStepLogId(Long stepLogId) {
        this.stepLogId = stepLogId;
    }

    public Integer getStepType() {
        return stepType;
    }

    public void setStepType(Integer stepType) {
        this.stepType = stepType;
    }

    public Date getStepCreateTime() {
        return stepCreateTime;
    }

    public void setStepCreateTime(Date stepCreateTime) {
        this.stepCreateTime = stepCreateTime;
    }

    public Date getStepUpdateTime() {
        return stepUpdateTime;
    }

    public void setStepUpdateTime(Date stepUpdateTime) {
        this.stepUpdateTime = stepUpdateTime;
    }

    public Long getStockLogId() {
        return stockLogId;
    }

    public void setStockLogId(Long stockLogId) {
        this.stockLogId = stockLogId;
    }

    public Date getStockCreateTime() {
        return stockCreateTime;
    }

    public void setStockCreateTime(Date stockCreateTime) {
        this.stockCreateTime = stockCreateTime;
    }

    public Date getStockUpdateTime() {
        return stockUpdateTime;
    }

    public void setStockUpdateTime(Date stockUpdateTime) {
        this.stockUpdateTime = stockUpdateTime;
    }

    public Long getBillId() {
        return billId;
    }

    public void setBillId(Long billId) {
        this.billId = billId;
    }

    public Date getBillCreateTime() {
        return billCreateTime;
    }

    public void setBillCreateTime(Date billCreateTime) {
        this.billCreateTime = billCreateTime;
    }

    public Date getBillUpdateTime() {
        return billUpdateTime;
    }

    public void setBillUpdateTime(Date billUpdateTime) {
        this.billUpdateTime = billUpdateTime;
    }

    @Override
    public String toString() {
        return "GoodsTimeData{" +
                "goodsId='" + goodsId + '\'' +
                ", collectId=" + collectId +
                ", goodsName='" + goodsName + '\'' +
                ", goodsSn='" + goodsSn + '\'' +
                ", printing='" + printing + '\'' +
                ", testTime=" + testTime +
                ", createTime=" + createTime +
                ", collectTime=" + collectTime +
                ", stepLogId=" + stepLogId +
                ", stepType=" + stepType +
                ", stepCreateTime=" + stepCreateTime +
                ", stepUpdateTime=" + stepUpdateTime +
                ", stockLogId=" + stockLogId +
                ", stockCreateTime=" + stockCreateTime +
                ", stockUpdateTime=" + stockUpdateTime +
                ", billId=" + billId +
                ", billCreateTime=" + billCreateTime +
                ", billUpdateTime=" + billUpdateTime +
                '}';
    }
}
